#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       fiweather
#       
#       Copyright 2010-2011 Aleksi Joakim Palomäki <aleksi@ajp.dy.fi>, Tuukka Ojala <tuukka.ojala@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
import urllib2
import sys
import FiWeathersrc

def help ():
	print 'Usage: fiweather city'
	print "Options:"
	print "-nofilt\t\t Don't filter output based on available information."
	print "-forec\t\t Show forecast.(incomplete!)"
	sys.exit (0)

try:
	location = sys.argv[1]
	'''Uncomment under for testing'''
	#location = "Helsinki"
except IndexError:
	help ()

#if sys.argv [1] == '-h' or sys.argv [1] == '--help':
#	help ()

'''Metrics'''
celsius = ' °C'
dewpoint = ' °C'
prosents = ' %'
windspeed = ' m/s'
windgust = ' m/s'
kilometers = ' km'
airpressure = ' hPa'
wateramount= ' mm'

#Do we filter?
filt = True
###Features in developement
try:
	if sys.argv[2]>0:
		if sys.argv[2] == '-forec':
			Y = FiWeathersrc.forec(location)
			print Y
			quit()
		elif sys.argv[2] == "-nofilt":
                        #We dont' want to filter :(
                        filt = False
except IndexError, e:
	pass
X = FiWeathersrc.finds(location)
#print X
'''Remove metrics from no info values'''
if X[0] == "Unknown city":
	city = none
else:
	city = X[0]
if X[1] == "No information.":
	celsius = ''
if X[2] == "No information.":
	dewpoint = ''
if X[3] == "No information.":
	pressure = ''
if X[4] == "No information." or X[4] == "Calm":
	windspeed = ''
if X[5] == "No information.":
	windgust = ''
if X[7] == "No information.":
	kilometers = ''
if X[8] == "No information.":
	airpressure = ''
if X[9] == "No information.":
	wateramount = ''
if location.capitalize () <> city:
	print "Invalid city."
	sys.exit (1)
	
Temperature 	= X[1] + celsius
Dew_Point   	= X[2] + dewpoint
Air_Humidity	= X[3] + prosents
Wind_speed	= X[4] + windspeed
Wind_gust	= X[5] + windgust
Cloud_level	= X[6] 
Visibility	= X[7] + kilometers
Air_Pressure	= X[8] + airpressure
Last_1h_Rain	= X[9] + wateramount	

#Ugly filtering "No information" away if wanted.
if filt == True:
        #print "filter true"
        if X[0].find("No information."):
                print 'Weather in %(city)s'% locals()
        if X[1].find("No information."):  
                print 'Temperature:\t %(Temperature)s'% locals()   
        if X[2].find("No information."):
                print 'Dew Point:\t %(Dew_Point)s'% locals()
        if X[3].find("No information."):
                print 'Air Humidity:\t %(Air_Humidity)s'% locals()
        if X[4].find("No information."):
                print 'Wind speed:\t %(Wind_speed)s'% locals()
        if X[5].find("No information."):
                print 'Wind gust:\t %(Wind_gust)s'% locals()
        if X[6].find("No information."):
                print 'Cloudness:\t %(Cloud_level)s'% locals()
        if X[7].find("No information."):
                print 'Visibility:\t %(Visibility)s'% locals()
        if X[8].find("No information."):
                print 'Air Pressure:\t %(Air_Pressure)s'% locals()
        if X[9].find("No information."):
                print 'Last 1h Rain:\t %(Last_1h_Rain)s'% locals()
        sys.exit(0)

###########################

Weather_All = '''Weather in %(city)s
Temperature:\t %(Temperature)s
Dew Point:\t %(Dew_Point)s
Air Humidity:\t %(Air_Humidity)s
Wind speed:\t %(Wind_speed)s
Wind gust:\t %(Wind_gust)s
Cloudness:\t %(Cloud_level)s
Visibility:\t %(Visibility)s
Air Pressure:\t %(Air_Pressure)s
Last 1h Rain:\t %(Last_1h_Rain)s
''' % locals()


print Weather_All
